package service;

import dao.DepartmentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Department;
import pojo.DepartmentPage;

import java.util.List;

/**
 * @author 郝雷蒙
 * @Description
 * @date 2020/12/8 18:50
 */
@Service
public class DepartmentServiceImpl implements DepartmentService{

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public DepartmentPage getAllDepartments(Integer page, Integer size) {
        Integer start = (page - 1) * size;

        List<Department> departments = departmentDao.selectAllDepartments(start,size);
        Integer count = departmentDao.selectDepartmentCount();

        Integer pageCount = count%size==0? count/size : count/size+1;

       DepartmentPage departmentPage = new DepartmentPage();
       departmentPage.setRowCount(count);
       departmentPage.setPageSize(size);
       departmentPage.setPageNum(page);
       departmentPage.setPageCount(pageCount);
       departmentPage.setDepartmentList(departments);

       return departmentPage;

    }

    @Override
    public Integer addOneDepartment(Department department) {
        return departmentDao.insertOneDepartment(department);
    }

    @Override
    public Integer setOneDepartment(Department department) {
        return departmentDao.updateOneDepartment(department);
    }

    @Override
    public Integer delOneDepartment(Integer id) {
        return departmentDao.deleteDepartmentByid(id);
    }


}
